package com.strongwell.awe.service.impl;

import com.alibaba.fastjson.JSON;
import com.strongwell.awe.model.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import com.strongwell.awe.dao.CmsCategoryDao;
import com.strongwell.awe.service.CmsCategoryService;



@Service("cmsCategoryServiceImpl")
public class CmsCategoryServiceImpl implements CmsCategoryService {
	@Autowired
	private CmsCategoryDao cmsCategoryDao;
	
	@Override
	public CmsCategoryEntity queryObject(Long id){
		return cmsCategoryDao.queryObject(id);
	}
	
	@Override
	public List<CmsCategoryEntity> queryList(Map<String, Object> map){
		return cmsCategoryDao.queryList(map);
	}
	
	@Override
	public int queryTotal(Map<String, Object> map){
		return cmsCategoryDao.queryTotal(map);
	}
	

	
	@Override
	public int delete(Long id){
		return cmsCategoryDao.delete(id);
	}
	
	@Override
	public int deleteBatch(Long[] ids){
		return cmsCategoryDao.deleteBatch(ids);
	}

    @Override
    public void save(CmsCategoryEntity cmscategory){
        Date date=new Date();
        cmscategory.setCreateTime(date);
			cmsCategoryDao.save(cmscategory);
    }

    @Override
    public int update(CmsCategoryEntity cmscategory){
        Date date=new Date();
        cmscategory.setUpdateTime(date);
        return cmsCategoryDao.update(cmscategory);
    }

    public int delById(Long id){
        Date date=new Date();
			CmsCategoryEntity cmscategory= new CmsCategoryEntity();
        cmscategory.setId(id);
        cmscategory.setDeleteTime(date);
        return cmsCategoryDao.update(cmscategory);
    }

    public int delPatch(String  cmscategorystr){
        String[] strs = cmscategorystr.split(",");
        if(strs!=null&&strs.length>0){
            for (String str : strs) {
                delById(Long.parseLong(str));
            }
        }
        return 0;
    }

    public CmsCategoryEntity queryName(String name){
        return cmsCategoryDao.queryName(name);
    }

    public List<CmsCategoryEntity> queryNameList(){
        return cmsCategoryDao.queryNameList();
    }

    public List<CmsCategoryEntity> queryCategoryList(){
        return cmsCategoryDao.queryCategoryList();
    }

    @Override
    public List<CmsCategoryEntity> selCategoryByParentId() {

        List<CmsCategoryEntity> data = cmsCategoryDao.selCategoryByParentId();
        return data;
    }


    public String selCategoryList1() {
        List<CmsCategoryEntity> list = cmsCategoryDao.selCategoryByParentId();
        List<cateTree> results = new ArrayList<>();

        if (list != null && list.size() > 0) {
            for (int i = 0; i < list.size(); i++) {
                if (list.get(i).getParentId() < 2) {
                    cateTree menu = new cateTree();
                    menu.setName(list.get(i).getName());
                    menu.setId(list.get(i).getId());
                    menu.setPid(list.get(i).getParentId());
                    menu.setHref(list.get(i).getHref());

                    List<cateTree> menus2 = new ArrayList<>();
                    for (int j = 0; j < list.size(); j++) {
                        if (list.get(j).getParentId() >1) {
                            if (list.get(j).getParentId().equals(list.get(i).getId())) {
                                cateTree menu2 = new cateTree();
                                menu2.setName(list.get(j).getName());
                                menu2.setId(list.get(j).getId());
                                menu2.setPid(list.get(j).getParentId());
                                menu2.setHref(list.get(j).getHref());
                                menus2.add(menu2);
                            }
                        }
                    }
                    menu.setChildren(menus2);
                    results.add(menu);

                }
            }
        }
        return JSON.toJSONString(results);

    }
    @Override
    public String selCategoryList() {
        List<CmsCategoryEntity> list = cmsCategoryDao.selCategoryByParentId();
        List<cateTree> results = new ArrayList<>();

        if (list != null && list.size() > 0) {
            for (int i = 0; i < list.size(); i++) {
                if (list.get(i).getParentId() == 0) {
                    cateTree menu=new cateTree();
                    menu.setName(list.get(i).getName());
                    menu.setId(list.get(i).getId());
                    menu.setPid(list.get(i).getParentId());
                    menu.setHref(list.get(i).getHref());
                    menu.setSpread("true");
                    results.add(menu);
                }
            }
            for (cateTree menu:results) {
                menu.setChildren(GetChild(menu.getId(),list));
            }
        }
        return JSON.toJSONString(results);

    }


    public List<cateTree> GetChild(Long id,List<CmsCategoryEntity> list){

        List<cateTree> results = new ArrayList<>();
        for (int i = 0; i < list.size(); i++) {
            if (list.get(i).getParentId() == id) {
                cateTree menu=new cateTree();
                menu.setName(list.get(i).getName());
                menu.setId(list.get(i).getId());
                menu.setPid(list.get(i).getParentId());
                menu.setHref(list.get(i).getHref());

                results.add(menu);
            }

        }
        for (cateTree menu:results) {
            menu.setChildren(GetChild(menu.getId(),list));
        }

        if (results.size() == 0) {
            return null;
        }
        return results;
    }
}
